package com.hmall.cart.listener;

import com.hmall.cart.controller.CartController;
import com.hmall.cart.service.ICartService;
import com.hmall.common.constants.MqConstants;
import com.hmall.common.utils.UserContext;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;


@Component
public class OrderStatusListener {
    @Autowired
    private ICartService cartService;

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(MqConstants.TRADE_SUCCESS_QUEUE),
                    exchange = @Exchange(name = MqConstants.TRADE_EXCHANGE_NAME),
                    key = MqConstants.Routing_SUCCESS_KEY
            )
    )
    public void listenOrderSuccess(Map<String, Object> map) {
        Collection<Long> itemIds = (Collection<Long>) map.get("itemIds");
        UserContext.setUser((Long) map.get("userId"));
        cartService.removeByItemIds(itemIds);
    }
}
